import { getInformations } from '../../services';
import actionTypes from './constants';

const startMarkAsRead = () => {
    return {
        type: actionTypes.START_MARK_AS_READ,
    }
}
const finishMarkAsRead = () => {
    return {
        type: actionTypes.FINISH_MARK_AS_READ,
    }
}


//获取消息列表
export const getInitialInformations = () => {
    return dispatch => {
        dispatch(startMarkAsRead());
        getInformations()
        .then(res => {
            dispatch({
                type: actionTypes.GET_INITIAL_INFORMATIONS,
                payLoad: {
                    list: res.data.list
                }
            })
            dispatch(finishMarkAsRead());
        })
        
    }
}
//标记某一条数据已读
export const markNotificationsAsReadById = (id) => {
    // console.log(id);
    return dispatch => {
        dispatch(startMarkAsRead());
        //这里模拟一个服务端的请求
        setTimeout(()=>{
            dispatch({
                type:actionTypes.MARK_NOTIFICATIONS_AS_READ_BY_ID,
                payLoad: {
                    id
                }
            })
            dispatch(finishMarkAsRead());
        }, 2000)
    }

}
//标记全部已读
export const markAllNotificationsAsRead = () => {
    return dispatch => {
        dispatch(startMarkAsRead());
        setTimeout(()=>{
            dispatch({
                type: actionTypes.MARK_ALL_NOTIFICATIONS_AS_READ,
            })
            dispatch(finishMarkAsRead());
        }, 2000)
    }
}

